﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Utils;


public partial class ajax_sms : FBasePage
{
    // Fields
    protected Maticsoft.BLL.Smssubscribe smsbll = new Maticsoft.BLL.Smssubscribe();
    protected Maticsoft.Model.Smssubscribe smsmodel = new Maticsoft.Model.Smssubscribe();
    public Maticsoft.BLL.System sysbll = new Maticsoft.BLL.System();
    public Maticsoft.Model.System sysmodel = new Maticsoft.Model.System();

    // Methods
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        string str = base.Request["action"];
        string mobile = Helper.GetString(base.Request["mobile"], string.Empty);
        string verifycode = Helper.GetString(base.Request["verifycode"], string.Empty);
        int @int = Helper.GetInt(base.Request["city_id"], 0);
        string secret = Helper.GetString(base.Request["secretcode"], string.Empty);
        string val = string.Empty;
        string str6 = str;
        if (str6 != null)
        {
            if (!(str6 == "subscribe"))
            {
                if (str6 == "unsubscribe")
                {
                    val = WebSiteHelper.LoadPageString("~/ajaxpage/ajax_dialog_smsun.aspx?mobile=" + mobile);
                    base.Response.Write(WebSiteHelper.GetJson(val, "dialog"));
                }
                else
                {
                    List<string> list;
                    if (str6 == "unsubscribecheck")
                    {
                        if (((this.Session["checkcode"] != null) && (verifycode != string.Empty)) && (this.Session["checkcode"].ToString().ToUpper() == verifycode.ToUpper()))
                        {
                            this.smsmodel = this.smsbll.GetMobile(mobile);
                            if (this.smsmodel != null)
                            {
                                if (this.smsmodel.Enable == "N")
                                {
                                    this.smsbll.deletebymobile(mobile);
                                    val = WebSiteHelper.LoadPageString("~/ajaxpage/ajax_dialog_smsunsuc.aspx?mobile=" + mobile);
                                }
                                else
                                {
                                    this.smsmodel = this.smsbll.GetMobile(mobile);
                                    this.sysmodel = this.sysbll.GetModel(1);
                                    val = WebSiteHelper.LoadPageString("~/ajaxpage/ajax_dialog_smscode.aspx?mobile=" + mobile);
                                    list = new List<string>();
                                    list.Add(mobile);
                                    base.SendSMS(list, this.sysmodel.sitename + "短信退订，您的手机号：" + mobile + " 短信退订功能认证码：" + this.smsmodel.Secret + "。");
                                }
                                base.Response.Write(WebSiteHelper.GetJson(val, "dialog"));
                            }
                            else
                            {
                                val = WebSiteHelper.LoadPageString("~/ajaxpage/ajax_dialog_smsunsuc.aspx?mobile=" + mobile);
                                base.Response.Write(WebSiteHelper.GetJson(val, "dialog"));
                            }
                        }
                        else
                        {
                            base.Response.Write(WebSiteHelper.GetJson("captcha_again();", "eval"));
                        }
                    }
                    else if (str6 == "subscribecheck")
                    {
                        if (mobile != string.Empty)
                        {
                            if ((this.Session["checkcode"] != null) && this.smsbll.isverifycode(verifycode, this.Session["checkcode"].ToString()))
                            {
                                this.smsmodel = this.smsbll.GetMobile(mobile);
                                if (this.smsmodel == null)
                                {
                                    this.smsmodel = new Maticsoft.Model.Smssubscribe();
                                    this.smsmodel.Mobile = mobile;
                                    this.smsmodel.City_id = @int;
                                    this.smsmodel.Secret = Helper.GetRandomString(6);
                                    this.smsmodel.Enable = "N";
                                    this.smsbll.Add(this.smsmodel);
                                }
                                else
                                {
                                    this.smsmodel.Secret = Helper.GetRandomString(6);
                                    this.smsbll.Update(this.smsmodel);
                                }
                                val = WebSiteHelper.LoadPageString("~/ajaxpage/ajax_dialog_smscode.aspx?mobile=" + mobile);
                                list = new List<string>();
                                list.Add(mobile);
                                this.sysmodel = this.sysbll.GetModel(1);
                                base.SendSMS(list, this.sysmodel.sitename + "短信订阅，您的手机号：" + mobile + " 短信订阅功能认证码：" + this.smsmodel.Secret + "。");
                                base.Response.Write(WebSiteHelper.GetJson(val, "dialog"));
                            }
                            else
                            {
                                base.Response.Write(WebSiteHelper.GetJson("captcha_again();", "eval"));
                            }
                        }
                    }
                    else if (str6 == "codeyes")
                    {
                        this.smsmodel = this.smsbll.GetMobile(mobile);
                        if (this.smsmodel != null)
                        {
                            if (!this.smsbll.Returnmobile(mobile))
                            {
                                base.Response.Write(WebSiteHelper.GetJson("非法访问！", "alert"));
                                base.Response.Write(WebSiteHelper.GetJson("X.boxClose();", "eval"));
                            }
                            else if (!this.smsbll.isSecret(secret, mobile))
                            {
                                base.Response.Write(WebSiteHelper.GetJson("短信认证码不正确，请重新输入！", "alert"));
                            }
                            else if (this.smsbll.isState(mobile))
                            {
                                this.smsbll.deletebymobile(mobile);
                                val = WebSiteHelper.LoadPageString("~/ajaxpage/ajax_dialog_smsunsuc.aspx");
                                base.Response.Write(WebSiteHelper.GetJson(val, "dialog"));
                            }
                            else
                            {
                                this.smsbll.UpdateState("Y", mobile);
                                val = WebSiteHelper.LoadPageString("~/ajaxpage/ajax_dialog_smssuc.aspx");
                                base.Response.Write(WebSiteHelper.GetJson(val, "dialog"));
                            }
                        }
                    }
                }
            }
            else
            {
                val = WebSiteHelper.LoadPageString("~/ajaxpage/ajax_dialog_smssub.aspx");
                base.Response.Write(WebSiteHelper.GetJson(val, "dialog"));
            }
        }
    }

}